﻿using System;
using System.Collections.Generic;
using Devbridge.IPolice.DomainModel;
using Devbridge.IPolice.ServiceContracts;
using Devbridge.IPolice.Services;

namespace Devbridge.IPolice.ConsoleApp
{
    public class Police : IDisposable
    {
        private readonly IPoliceOfficerService policeOfficerService;
        private readonly IOffenderService offenderService;
        private readonly ICrimeService crimeService;

        public Police()
        {
            offenderService = new OffenderService();
            policeOfficerService = new PoliceOfficerService();
            crimeService = new CrimeService();
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            offenderService.Dispose();
            policeOfficerService.Dispose();
            crimeService.Dispose();
        }

        static void Main(string[] args)
        {
            using (var police = new Police())
            {
                GetPoliceOfficersWithCrimes(police);

                Console.ReadLine();

                GetThisYearCrimes(police);

                Console.ReadLine();

                UpdatePoliceOfficerPhones(police);

                Console.ReadLine();

                DisplayOffenders(police, "Jonas");

                Console.ReadLine();

                DisplayPoliceOfficers(police, "Petras");

                Console.ReadLine();

                AddNewOfficer(police);

                DisplayPoliceOfficers(police);

                Console.ReadLine();

                RemoveOfficer(police);

                DisplayPoliceOfficers(police);

                Console.ReadLine();

                RemoveOfficer(police);

                DisplayPoliceOfficers(police);
            }
        }

        private static void GetPoliceOfficersWithCrimes(Police police)
        {
            Console.WriteLine("Get police officers with crimes:");
            police.policeOfficerService.GetPoliceOfficerWithCrimes();
        }

        private static void GetThisYearCrimes(Police police)
        {
            var from = DateTime.Now.AddMonths(-6);
            var to = DateTime.Now.AddMonths(6);
            Console.WriteLine("Get crimes from {0} to {1}:", from, to);
            police.crimeService.GetCrimes(from, to);
        }

        private static void UpdatePoliceOfficerPhones(Police police)
        {
            Console.WriteLine("Updates police officer phones:");
            police.policeOfficerService.UpdatePhoneNumberForAll();
        }

        private static void DisplayOffenders(Police police, string name)
        {
            Console.WriteLine("Offenders with name like '{0}':", name);
            var offenders = police.offenderService.GetByFirstName(name);

            foreach (var offender in offenders)
            {
                Console.WriteLine(offender);
            }
        }

        private static void DisplayPoliceOfficers(Police police, string name = null)
        {
            Console.WriteLine("Police officers with name '{0}':", name);
            IList<PoliceOfficer> policeOfficers = police.policeOfficerService.GetPoliceOfficers(name);

            foreach (var policeOfficer in policeOfficers)
            {
                Console.WriteLine(policeOfficer);
            }
        }

        private static void AddNewOfficer(Police police)
        {
            var newPoliceOfficer = new PoliceOfficer()
                                       {
                                           FirstName = "Petras",
                                           LastName = "Pavardenis"
                                       };
            police.policeOfficerService.AddPoliceOfficer(newPoliceOfficer);
        }

        private static void RemoveOfficer(Police police)
        {
            police.policeOfficerService.RemovePoliceOfficerByLastName("Jonaitis");
        }
    }
}
